<?php
declare (strict_types = 1);

namespace app\admin\controller\setting;

use app\admin\controller\AuthController;
use bbweb\services\UtilService as Util;
use app\admin\model\system\{SystemAdmin as AdminModel,SystemRole,SystemMenus};
use think\facade\Config;
use think\facade\Db;
use think\Request;

class SystemAdmin extends AuthController
{
    /*
     * 显示列表
     */
    public function index(Request $req)
    {
        $groups = SystemRole::getSelect(0,2);
        if ($req->isAjax()) {
            $data = [];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 15);
            $query = Db::name('system_admin');
            // 条件搜索
            $roleid = $req->param('roleid');
            if (!is_null($roleid) && strlen($roleid)) {
                $path = Db::name('system_role')->where('id',$roleid)->value('path');
                $query->where('roleid','in',$path);
            }
            $type = $req->param('type');
            if (!is_null($type) && strlen($type)) {
                $query->where('type','=',$type);
            }
            $name = $req->param('name');
            if (!is_null($name) && strlen($name)) {
                $query->where('real_name','like',"%$name%");
            }
            $phone = $req->param('phone');
            if (!is_null($phone) && strlen($phone)) {
                $query->where('phone','like',"%$phone%");
            }
            $query->where('is_del',0);
            $query->order('id DESC');
            $data['count'] = $query->count('id');
            $data['data'] = $query->page($page,$limit)->select()->toArray();
            $data['code'] = 0;
            $data['msg'] = '';
            foreach ($data['data'] as &$val) {
                $val['group'] = isset($groups[$val['roleid']]) ? $groups[$val['roleid']] : '--/--';
                $val['last_time'] = $val['last_time'] ? date('Y-m-d H:i',$val['last_time']) : '';
            }
            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));
        // 显示页面
        $this->assign('groups', $groups);
        $this->assign('sonAuth', $sonAuth);
        return $this->fetch();
    }

    /*
     * 新建
     */
    public function add(Request $req)
    {
        if($req->isPost()) {
            $data = Util::postMore([
                'account',
                'conf_password',
                'password',
                'real_name',
                'phone',
                ['roleid', 0],
                ['status', 0]
            ]);
            if (!$data['account']) $this->error('请输入管理员账号');
            if (!$data['roleid']) $this->error('请选择管理员身份');
            if (!$data['password']) $this->error('请输入管理员登陆密码');
            if ($data['password'] != $data['conf_password']) $this->error('两次输入密码不相同');
            if (AdminModel::be($data['account'], 'account')) $this->error('管理员账号已存在');
            $data['password'] = md5(md5($data['password']).Config::get('hello.secret'));
            unset($data['conf_password']);
            $res = AdminModel::create($data);
            if($res) $this->success();
            else $this->error();
        }
        $groups = SystemRole::getSelect(0,1);
        $this->assign('groups', $groups);
        // 显示页面
        return $this->fetch();
    }

    /*
     * 编辑
     */
    public function edit(Request $req)
    {
        $id = $req->param('id/d');
        if (!$id) $this->error('参数错误','',-1);
        $admin = AdminModel::get($id);
        if (!$admin) $this->error('数据不存在!','',-1);
        if($req->isPost()) {
            $data = [];
            if($req->param('change/d')) {
                $data['status'] = $req->param('status/d');
            } else {
                $data = Util::postMore([
                    'account',
                    'conf_password',
                    'password',
                    'real_name',
                    'phone',
                    ['roleid', 0],
                    ['status', 0]
                ]);
                if (!$data['account']) $this->error('请输入管理员账号');
                if (!$data['roleid']) $this->error('请选择管理员身份');
                if (!empty($data['password']) && $data['password'] != $data['conf_password']) $this->error('两次输入密码不相同');
                if (AdminModel::where('account', $data['account'])->where('id', '<>', $id)->count()) $this->error('管理员账号已存在');
                if (!empty($data['password'])) {
                    $data['password'] = md5(md5($data['password']) . Config::get('hello.secret'));
                } else {
                    unset($data['password']);
                }
                unset($data['conf_password']);
            }
            $data['update_at'] = time();
            $res = AdminModel::where('id', $id)->update($data);
            if($res) $this->success();
            else $this->error();
        }
        $groups = SystemRole::getSelect(0,1);
        $this->assign('groups', $groups);
        $this->assign('admin', $admin);
        return $this->fetch();
    }

    /*
     * 删除
     */
    public function del(Request $req)
    {
        $id = $req->param('id/d');
        $bool = AdminModel::del($id);
        if($bool) {
            $this->success('操作成功！');
        } else {
            $this->error(AdminModel::getErrorInfo('删除失败,请稍候再试!'));
        }
    }
}
